package com.doge.calcalbum.business.vip

import androidx.fragment.app.FragmentActivity
import com.doge.calcalbum.R
import com.doge.calcalbum.analysis.analysisEvent
import com.doge.calcalbum.analysis.vip_end_remind
import com.doge.calcalbum.analysis.vip_free_remind
import com.doge.calcalbum.analysis.vip_remind
import com.doge.calcalbum.api.*
import com.doge.calcalbum.api.model.UserInfo
import com.doge.calcalbum.business.account.AccountHelper
import com.doge.calcalbum.business.account.AppUserViewModel
import com.doge.calcalbum.business.skin.SkinHelper
import com.doge.calcalbum.db.model.Album
import com.doge.calcalbum.utils.*

object VipHelper {
    private const val folder = "dlknr"
    private const val key = "kg"
    private const val keyFirstVideo = "fv"
    private const val keyFirstPicture = "fp"
    private const val keyFirstSetPwd = "fsp"
    private val self get() = AccountHelper.self

    fun tryRestoreSkin() {
        if (!self.isVip && (SkinHelper.usingSkinId != 0 && SkinHelper.usingSkinId != 1)) {
            SkinHelper.setup(0)
        }
    }

    fun tryShowWhenAppOpen(activity: FragmentActivity) {
        log("tryShowWhenAppOpen activity $activity self $self")
        if (self.isVipForever) return
        if (self.id == 0) return
//        if (self.canGetFreeVip == 1) return
        if (!AppGuideUtils.hasGuideAlbum) return
        if (!AppGuideUtils.hasGuideSetPwd) return

        val lastRemind = spReadString(key, null, folder)
        val today = System.currentTimeMillis().timeDesc("yyyyMMdd")
        if (today != lastRemind) {
            spWrite(key, today, folder)
            if (self.isVipJust7Day || self.wasVip7Day) {
                showLeftTimeDialog(activity)
            } else if (!self.isVip){
                showBuyDialog("appOpen", activity)
            }
        }
    }

    fun tryShowWhenUploadFileFirstTime(type: Int) {
        if (!AppGuideUtils.hasGuideSetPwd) return
        val activity: FragmentActivity = Activities.top ?: return

        if (type == Album.TYPE_VIDEO) {
            val lastRemind = spReadBoolean(keyFirstVideo, false, folder)
            if (!lastRemind) {
                spWrite(keyFirstVideo, true, folder)
                if (!self.isVip) {
                    showBuyDialog("firstVideo", activity)
                }
            }
        } else {
            val lastRemind = spReadBoolean(keyFirstPicture, false, folder)
            if (!lastRemind) {
                spWrite(keyFirstPicture, true, folder)
                if (!self.isVip) {
                    showBuyDialog("firstPicture", activity)
                }
            }
        }
    }

    fun tryShowWhenSetPwdFirstTime(activity: FragmentActivity) {
        if (AppGuideUtils.hasGuideSetPwd) {
            val lastRemind = spReadBoolean(keyFirstSetPwd, false, folder)
            if (!lastRemind) {
                spWrite(keyFirstSetPwd, true, folder)
                if (!self.isVip) {
                    showBuyDialog("firstSetPwd", activity)
                }
            }
        }
    }

    private fun showLeftTimeDialog(activity: FragmentActivity) {
        analysisEvent(vip_end_remind)
        VipLeftTimeInfoDialog.Builder(activity)
            .setLeftTimeSec((self.vipExpireLeftTime ?: 0))
            .setCallback { _, _ ->
                analysisEvent("${vip_end_remind}_to_vip")
                VipBuyActivity.start(Activities.top)
            }
            .create().show()
    }

    fun showBuyDialog(refer: String, context: FragmentActivity) {
        log("showBuyDialog refer $refer activity $context")

//        if (self.canGetFreeVip == 1) {
//            showGetFreeVipDialog()
//            return
//        }

        analysisEvent("${vip_remind}_$refer")
        VipInfoDialog.Builder(context)
            .setCallback(callback = { _, _ ->
                analysisEvent("${vip_remind}_${refer}_to_vip")
                VipBuyActivity.start(Activities.top)
            }, cancelCallback = { _, _ ->
                showGetFreeVipDialog()
            })
            .create().show()
    }

    private fun showGetFreeVipDialog() {
        if (AccountHelper.self.canGetFreeVip != 1) return
        val context = Activities.top ?: return
        analysisEvent(vip_free_remind)
        log("showGetFreeVipDialog")
        VipInfoDialog.Builder(context)
            .setConfirmText(R.string.vip_free_get)
            .setCallback (callback = { _, _ ->
                requestGetFreeVip()
            }, cancelCallback = { _, _ ->
            })
            .create().show()
    }

    private fun requestGetFreeVip() {
        val context = Activities.top ?: return
        api.getFreeVip()
            .map(ApiResultMapTransformer())
            .subscribeOnIO()
            .observeOnMain()
            .subscribe(object : SimpleObserverAdapter<UserInfo>(context, context) {
                override fun onNext(t: UserInfo) {
                    toast(R.string.vip_free_got)
                    AppUserViewModel.get().updateSelf(t)
                }
            })
    }
}